home *** CD-ROM | disk | FTP | other *** search
/ CD/PC Actual 76 / DVD Actual 1 Marzo 2003.iso / Trial / TurboCAD 7.1 Pro / Data.Cab / F29013_SdiDialog.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-10  |  17.7 KB  |  848 lines

  1. /******************************************************************/
  2. /*                                                                */
  3. /*                      TurboCAD for Windows                      */
  4. /*                   Copyright (c) 1993 - 1999                    */
  5. /*             International Microcomputer Software, Inc.         */
  6. /*                            (IMSI)                              */
  7. /*                      All rights reserved.                      */
  8. /*                                                                */
  9. /******************************************************************/
  10. // SdiDialog.cpp : implementation file
  11. //
  12.  
  13.  
  14. #include "stdafx.h"
  15. #include "InsSmObj.h"
  16. #include "SdiDialog.h"
  17.  
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23.  
  24. //extern CSdiDialog CSDlg;
  25.  
  26. const    HRESULT hRes = S_OK;
  27. const IID LIBID_IMSIGXLib = {0x6A481400,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  28. const IID IID_PickResult = {0x6A481123,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  29. //const IID IID_Tool = {0x6A48112C,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  30. const IID IID_Graphics = {0x6A48110A, 0xE531, 0x11CF, {0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  31. const IID IID_View = {0x6A481110, 0xE531, 0x11CF, {0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  32. const IID IID_Window = {0x6A481124, 0xE531, 0x11CF, {0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF} };
  33. const CLSID CLSID_Application  = {0x6A481001,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  34. const CLSID CLSID_XGraphic  = {0x6A481803,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  35.  
  36.  
  37. //const CLSID CLSID_XApplication = {0x6A481801,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  38. //const IID IID_IApplication = {0x6A481101,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  39. static const IID IID_IAppEvents =
  40. { 0x6A481301, 0xE531, 0x11CF, { 0xA1, 0x15, 0x0, 0xA0, 0x24, 0x15, 0x8D, 0xAF } };
  41.  
  42. /////////////////////////////////////////////////////////////////////////////
  43. // CSdiDialog dialog
  44.  
  45.     long a = 0;
  46.  
  47. BEGIN_MESSAGE_MAP(CSdiDialog, CDialog)
  48.     //{{AFX_MSG_MAP(CSdiDialog)
  49.     ON_BN_CLICKED(ID_CLOSE, OnClose)
  50.     ON_BN_CLICKED(IDC_PROPERTIES, OnProperties)
  51.     ON_WM_DESTROY()
  52.     ON_WM_SYSCOMMAND()
  53.     //}}AFX_MSG_MAP
  54. ///    ON_LBN_DBLCLK(IDC_LIST1, onDoubleClick)
  55. END_MESSAGE_MAP()
  56.  
  57.  
  58. BEGIN_DISPATCH_MAP(CSdiDialog, CDialog)
  59.     //{{AFX_DISPATCH_MAP(CTCEventsApp)
  60.     DISP_FUNCTION(CSdiDialog, "MouseDown", MouseDown, VT_EMPTY, VTS_DISPATCH VTS_DISPATCH VTS_DISPATCH VTS_I2 VTS_I4 VTS_I4 VTS_I4 VTS_PBOOL)
  61.     DISP_FUNCTION(CSdiDialog, "MouseUp", MouseUp, VT_EMPTY, VTS_DISPATCH VTS_DISPATCH VTS_DISPATCH VTS_I2 VTS_I4 VTS_I4 VTS_I4 VTS_PBOOL)
  62.     DISP_FUNCTION(CSdiDialog, "MouseMove", MouseMove, VT_EMPTY, VTS_DISPATCH VTS_DISPATCH VTS_DISPATCH VTS_I4 VTS_I4 VTS_I4 VTS_PBOOL)
  63. //    DISP_FUNCTION(CSdiDialog, "Drop", Drop, VT_EMPTY, VTS_DISPATCH VTS_DISPATCH VTS_DISPATCH VTS_DISPATCH)
  64.     DISP_FUNCTION(CSdiDialog,    "DrawingBeforeClose", DrawingBeforeClose, VT_EMPTY, VTS_DISPATCH VTS_PBOOL)
  65.  
  66.     
  67.     //}}AFX_DISPATCH_MAP
  68. END_DISPATCH_MAP()
  69.  
  70.  
  71. BEGIN_INTERFACE_MAP(CSdiDialog, CDialog)
  72.     INTERFACE_PART(CSdiDialog, IID_IAppEvents, Dispatch)
  73. END_INTERFACE_MAP()
  74.  
  75.  
  76. void CSdiDialog::MouseDown(LPDISPATCH WhichDrawing, LPDISPATCH WhichView, LPDISPATCH WhichWindow, short Button, long Shift, long X, long Y, IMSI_BOOL* pbCancel)
  77. {
  78.     *pbCancel = FALSE;
  79.  
  80. }
  81.  
  82. void CSdiDialog::MouseUp(LPDISPATCH WhichDrawing, LPDISPATCH WhichView, LPDISPATCH WhichWindow, short Button, long Shift, long X, long Y, IMSI_BOOL* pbCancel)
  83. {
  84.  
  85.     
  86.     COleVariant varItem = (0L);
  87.     COleVariant varOptional(varMissing);
  88.  
  89.     IDrawing *pIDwgTrg = NULL; /// current drawing
  90.     pIDwgTrg = (IDrawing*) WhichDrawing;
  91.     pIDwgTrg->AddRef();
  92.     Graphics *pGrs = NULL;
  93.  
  94.     *pbCancel = FALSE;
  95.     CPoint point;
  96.     point.x = X;
  97.     point.y = Y;
  98.  
  99.     
  100.     try 
  101.     {
  102.         if(m_pDragGraphic != 0)
  103.         {
  104.  
  105.             hRes = pIDwgTrg->get_Graphics(&pGrs); // get current graphic collection
  106.             CHECK_HRESULT(hRes)
  107.  
  108.             CViewWnd *pPreview = (CViewWnd *) (GetDlgItem(IDC_PREVIEW));
  109.  
  110.             hRes = pGrs->AddGraphic(m_pDragGraphic, varOptional, varOptional);
  111.             CHECK_HRESULT(hRes)
  112.  
  113.             hRes = m_pDragGraphic->Update();
  114.             CHECK_HRESULT(hRes)
  115.  
  116.             if(m_pView != NULL)
  117.             {
  118.                 m_pView->Release();
  119.             }
  120.                 
  121.             m_pView = (View*) WhichView;
  122.             m_pView->AddRef();
  123.  
  124.             EndDrag(point);
  125.  
  126.             hRes = m_pView->Refresh();
  127.             CHECK_HRESULT(hRes)
  128.  
  129.             m_FirstClick = FALSE;    
  130.         }    
  131.     }        
  132.     catch (...)
  133.     {
  134.         TRACE_EXCEPTION("CSdiDialog::MouseUp")
  135.     }
  136.     
  137.     if(m_pDragGraphic != NULL)
  138.     {
  139.         m_pDragGraphic->Release();
  140.         m_pDragGraphic = NULL;
  141.     }
  142.     if (m_pView != NULL)
  143.     {
  144.         m_pView->Release();
  145.         m_pView = NULL;
  146.     }
  147.  
  148.     if (pGrs != NULL)
  149.     {
  150.         pGrs->Release();
  151.         pGrs = NULL;
  152.     }
  153.  
  154.     if(pIDwgTrg != NULL) 
  155.     {
  156.         pIDwgTrg->Release();
  157.         pIDwgTrg = NULL;
  158.     }
  159.     
  160.  
  161.  
  162. }
  163.  
  164. void CSdiDialog::MouseMove(LPDISPATCH WhichDrawing, LPDISPATCH WhichView, LPDISPATCH WhichWindow, long Shift, long X, long Y, IMSI_BOOL* pbCancel)
  165. {
  166. //    long i;
  167.     CPoint point;
  168.     point.x = X;
  169.     point.y = Y;
  170.  
  171.     *pbCancel = FALSE;
  172.     
  173.  
  174.     View *pViewCur = NULL;
  175.     COleVariant varItem = (1L);
  176.     
  177.     try 
  178.     {
  179.  
  180.         if (m_FirstClick == TRUE) 
  181.         {
  182.     
  183.             HCURSOR hOC = ::SetCursor(::LoadCursor(NULL,IDC_WAIT));
  184.  
  185.             if(m_pView != NULL)
  186.             {
  187.                 //m_pView->Release();
  188.                 RELEASE(m_pView);
  189.             }
  190.             m_pView = (View *)WhichView;
  191.             m_pView->AddRef();
  192.  
  193.             CViewWnd *pPreview = (CViewWnd *) (GetDlgItem(IDC_PREVIEW));
  194.  
  195.             
  196.             
  197.             hRes = pPreview->m_pPreviewGraphic->Duplicate(&m_pDragGraphic);
  198.             CHECK_HRESULT(hRes)
  199.             m_pDragGraphic->Release();
  200.             m_pDragGraphic = NULL;
  201.                 
  202.             hRes = pPreview->m_pPreviewGrs->Remove(&varItem, &m_pDragGraphic);
  203.             CHECK_HRESULT(hRes)
  204.             
  205.             hRes = m_pDragGraphic->put_ID(0);
  206.             CHECK_HRESULT(hRes)
  207.  
  208.             m_FirstClick = FALSE;
  209.             ::SetCursor(hOC);
  210.  
  211.             BeginDrag(point);
  212.         }
  213.             
  214.             
  215.         else 
  216.         {
  217.             if (m_pDragGraphic != NULL && !m_bDragging)/// aalb && (nFlags & MK_LBUTTON))
  218.             {
  219.                 if (!BeginDrag(point))
  220.                 {
  221.                     m_pDragGraphic->Release();
  222.                     m_pDragGraphic = NULL;
  223.                 }
  224.             }
  225.             else if (m_bDragging)
  226.             {
  227.  
  228.                 pViewCur = (View *)WhichView;
  229.                 pViewCur->AddRef();
  230.                 if (pViewCur != m_pView) // if view is changed then restart drag
  231.                 {
  232.                     if(m_pView != NULL)
  233.                     {
  234.                         RELEASE(m_pView);
  235.                     }
  236.  
  237.                     m_pView = (View *)WhichView;//pViewCur;
  238.                     m_pView->AddRef();
  239.                     pViewCur->Release();
  240.                     pViewCur = NULL;
  241.  
  242.                     RestartDrag(point);                
  243.  
  244.                 }
  245.                 Drag(point);
  246.             }    
  247.             
  248.         }
  249.     }    
  250.     catch (...)
  251.     {
  252.         TRACE_EXCEPTION("CSdiDialog::OnMouseMove")
  253.     }
  254.     
  255.         
  256.         
  257.         
  258.  
  259. }
  260.  
  261.  
  262. // Get the running instance of the TurboCAD local server object from the ROT.
  263.  
  264.  
  265. void CSdiDialog::DisconnectEvents()
  266. {
  267.     //HRESULT hRes = S_OK;
  268.  
  269.     if (m_pIApplication == NULL || m_dwEventConnection == 0)
  270.         return;
  271.     try 
  272.     {
  273.  
  274.         HRESULT    hRes = m_pIApplication->DisconnectEvents(m_dwEventConnection);
  275.         CHECK_HRESULT(hRes)
  276.         if (FAILED(hRes))
  277.         {
  278.             
  279.             //AfxMessageBox("Connection not broken.");//# Localizable string#
  280.             AfxMessageBox(IDS_STRING106);
  281.             return;
  282.     
  283.         }
  284.         m_dwEventConnection = 0;
  285.  
  286.     }
  287.  
  288.     catch (...)
  289.     {
  290.         TRACE_EXCEPTION("CSdiDialog::DisconnectEvents")
  291.     }
  292.     if(m_pIApplication != NULL)
  293.     {
  294.         m_pIApplication->Release(); 
  295.         m_pIApplication = NULL;
  296.     }
  297.  
  298.  
  299. }
  300.  
  301. //------------------------
  302.  
  303. CSdiDialog::CSdiDialog(CInsSmObj* pTool, CWnd* pParent, /*=NULL*/RegenMethods *pRgs)
  304.     : CDialog(CSdiDialog::IDD, pParent),
  305.     m_dwEventConnection(0),
  306.     m_dwEventMask(~0UL),
  307.     m_pIApplication(NULL),
  308.     m_ptMouseDown(CPoint(0, 0)),
  309.     m_FirstClick(FALSE),
  310.     m_bDragging(FALSE),
  311.     m_pView(NULL),
  312.     m_pDragGraphic(NULL)
  313.  
  314. {
  315.  
  316.     //{{AFX_DATA_INIT(CSdiDialog)
  317.     m_listbox = _T("");
  318.     //}}AFX_DATA_INIT
  319.     m_pTool = pTool;
  320.     m_pTool->AddRef();
  321.     m_pTool->m_bRunned = TRUE;
  322.     m_pRegMets = pRgs;
  323.     ADDREF(m_pRegMets);
  324. }
  325.  
  326.  
  327. void CSdiDialog::DoDataExchange(CDataExchange* pDX)
  328. {
  329.     CDialog::DoDataExchange(pDX);
  330.     //{{AFX_DATA_MAP(CSdiDialog)
  331.     DDX_LBString(pDX, IDC_LIST1, m_listbox);
  332.     //}}AFX_DATA_MAP
  333. }
  334.  
  335.  
  336.  
  337.  
  338. /////////////////////////////////////////////////////////////////////////////
  339. // CSdiDialog message handlers
  340.  
  341. BOOL CSdiDialog::OnInitDialog() 
  342. {
  343.     CDialog::OnInitDialog();
  344.     
  345.     m_list.SubclassDlgItem( IDC_LIST1 , this );
  346.     CToolList *pList = (CToolList *)GetDlgItem(IDC_LIST1);
  347.  
  348.     m_wndView.SubclassDlgItem(IDC_PREVIEW, this);
  349.     CViewWnd *pView = (CViewWnd *)GetDlgItem(IDC_PREVIEW);
  350.  
  351.     COleVariant varItem;
  352.     CString cstrDesc;
  353.     CString cstrName;
  354.  
  355.     HRESULT hRes = S_OK;
  356.     RegenMethod *pRegMet = NULL;
  357.     BSTR bstrDescription = NULL;
  358.     BSTR bstrName = NULL;
  359.     ImsiRegenMethodType regenType;
  360.     EnableAutomation();
  361.  
  362.     long NumRegs = 0;
  363.     
  364.     try
  365.     {
  366.         hRes = m_pRegMets->get_Count(&NumRegs);
  367.         CHECK_HRESULT(hRes)
  368.  
  369.         for(long i = 0;i < NumRegs; i++)
  370.         {
  371.             varItem = i;
  372.  
  373.             hRes = m_pRegMets->get_Item(&varItem,&pRegMet);
  374.             CHECK_HRESULT(hRes)
  375.  
  376.             hRes = pRegMet->get_Type(®enType);
  377.             CHECK_HRESULT(hRes)
  378.  
  379. //            hRes = pRegMet->get_Description(&bstrDescription);
  380.             
  381. //            if (SUCCEEDED(hRes))
  382.             if(regenType == imsiAutomation)
  383.             {
  384.                     hRes = pRegMet->get_Description(&bstrDescription);
  385.                     if (SUCCEEDED(hRes))
  386.                     {
  387.                         cstrDesc = bstrDescription;
  388.                         ::SysFreeString(bstrDescription);
  389.                         bstrDescription = NULL;        
  390.                     
  391. //                        if(cstrDesc != "HatchWizard.HatchWizard.1" || cstrDesc != "Smart.Connection.1")
  392. //                        if(cstrDesc != "HatchWizard.HatchWizard.1" || cstrDesc != "Smart.Connection.1" )
  393. //                        {
  394.                             hRes = pRegMet->get_Name(&bstrName);
  395.                             CHECK_HRESULT(hRes)
  396.                         
  397.                             cstrName = bstrName;
  398.                             ::SysFreeString(bstrName);
  399.                             bstrName = NULL;        
  400.                             if(cstrName == "Foundation.House" || cstrName == "AutoDim.House" || cstrName == "Schedule.Block")
  401.                             {
  402.                             }
  403.                             else
  404.                             {
  405.                                 pList->AddString(cstrName);
  406.                             }
  407.                             
  408. //                        }
  409.                     }
  410.             }
  411.  
  412.         }
  413.         if(pList->SetCurSel(0) != LB_ERR)
  414.         {
  415.  
  416.             pList->GetText(0 ,(pList->m_cstrCurSelected));
  417.  
  418.             pView->CreatePreview();
  419.             pView->DoPreview();            
  420.             ConnectEvents();
  421.     
  422.         }
  423.  
  424.     }
  425.     catch (...)
  426.     {
  427.         TRACE_EXCEPTION("CSdiDialog::OnInitDialog")
  428.     }
  429.  
  430.     if (pRegMet != NULL)
  431.     {
  432.         pRegMet->Release();
  433.         pRegMet = NULL;
  434.     }
  435.  
  436.     if (bstrDescription != NULL)
  437.         ::SysFreeString(bstrDescription);
  438.  
  439.     if (bstrName != NULL)
  440.         ::SysFreeString(bstrName);
  441.  
  442.     return TRUE;  // return TRUE unless you set the focus to a control
  443.                   // EXCEPTION: OCX Property Pages should return FALSE
  444. }
  445.  
  446. void CSdiDialog::PostNcDestroy() 
  447. {
  448.     CDialog::PostNcDestroy();
  449.     RELEASE(m_pRegMets)
  450.     delete this;
  451. }
  452.  
  453. void CSdiDialog::OnClose() 
  454. {
  455.     // TODO: Add your message handler code here and/or call default
  456.  
  457.     if(m_dwEventConnection != 0)
  458.         DisconnectEvents();
  459.     m_pTool->m_bRunned = FALSE;
  460.         if(m_pTool != NULL)
  461.         {
  462.             m_pTool->Release ();
  463.             m_pTool = NULL;
  464.         }
  465.  
  466.         if(m_pRegMets != NULL)
  467.         {
  468.             m_pRegMets->Release();
  469.             m_pRegMets = NULL;
  470.         }
  471.  
  472.     CViewWnd *pView = (CViewWnd *)GetDlgItem(IDC_PREVIEW);
  473.     pView->ClearAll();
  474.  
  475.     
  476.     CDialog::OnClose();
  477.     DestroyWindow();
  478.  
  479.  
  480. }
  481.  
  482.  
  483.  
  484. void CSdiDialog::ConnectEvents()
  485. {
  486.     HRESULT hRes = S_OK;
  487.  
  488.     if (m_dwEventConnection != 0)
  489.     {
  490.         //AfxMessageBox("Already connected.");//# Localizable string#
  491.         AfxMessageBox(IDS_STRING107);
  492.         return;
  493.     }
  494.     
  495.     m_pIApplication = NULL;
  496.     try
  497.     {
  498.         hRes = m_pRegMets->get_Application(&m_pIApplication);
  499.         CHECK_HRESULT(hRes)
  500.  
  501.         if (m_pIApplication == NULL)
  502.         {
  503.                 //AfxMessageBox("Can't get a TurboCAD application!");//# Localizable string#
  504.                 AfxMessageBox(IDS_STRING108);
  505.                 return;
  506.         }
  507.  
  508.  
  509.         
  510.         IDispatch* pUnkEventSink = GetIDispatch(TRUE);
  511.  
  512.         UpdateData(TRUE);
  513.         COleVariant var((long)m_dwEventMask);
  514.         hRes = m_pIApplication->ConnectEvents(pUnkEventSink,
  515.             &var,
  516.             (long*)&m_dwEventConnection);
  517.         CHECK_HRESULT(hRes)
  518.         if (FAILED(hRes))
  519.         {
  520.             //AfxMessageBox("Connection not established.");//# Localizable string#
  521.             AfxMessageBox(IDS_STRING109);
  522.             return;
  523.         }
  524.     }
  525.     catch (...)
  526.     {
  527.         TRACE_EXCEPTION("CSdiDialog::ConnectEvents")
  528.     }
  529.  
  530.  
  531.  
  532.  
  533. }
  534.  
  535.  
  536.  
  537. void CSdiDialog::OnProperties() 
  538. {
  539.     // TODO: Add your control notification handler code here
  540.  
  541.     VARIANT varIndex;
  542.     varIndex.vt = VT_I4;
  543.     varIndex.lVal = 0;
  544.     Graphics* pGrs = NULL;
  545.  
  546.  
  547.     CViewWnd *pView = (CViewWnd *) (GetDlgItem(IDC_PREVIEW));
  548.  
  549.     RegenMethod * pCurRegenMethod = NULL;
  550.     
  551.     short i = 0;
  552.     try
  553.     {
  554.         hRes = pView->m_pPreviewGraphic->get_Graphics(&pGrs);
  555.         CHECK_HRESULT(hRes)
  556.  
  557.         hRes = pView->m_pPreviewGraphic->get_RegenType(&pCurRegenMethod);
  558.         CHECK_HRESULT(hRes)
  559.  
  560.         hRes = pCurRegenMethod->RunPropertyPage(pGrs, &varIndex, &i );
  561.         CHECK_HRESULT(hRes)
  562.  
  563.         hRes = pView->m_pPreviewView->ZoomToExtents();
  564.         CHECK_HRESULT(hRes)
  565.  
  566.         pView->Invalidate();
  567.     }
  568.     catch (...)
  569.     {
  570.         TRACE_EXCEPTION("CSdiDialog::OnProperties")
  571.     }
  572.  
  573.  
  574.     if (pCurRegenMethod != NULL)
  575.     {
  576.         pCurRegenMethod->Release();
  577.         pCurRegenMethod = NULL;
  578.     }
  579.     
  580.     if(pGrs != NULL)
  581.     {
  582.         pGrs->Release();
  583.         pGrs = NULL;
  584.     }
  585.  
  586. }
  587.  
  588.  
  589.  
  590. void CSdiDialog::OnSysCommand(UINT nID, LPARAM lParam )
  591. {
  592.     if (nID == SC_CLOSE)
  593.     {
  594.         if(m_dwEventConnection != 0)
  595.         DisconnectEvents();
  596.     
  597.         m_pTool->m_bRunned = FALSE;
  598.         if(m_pTool != NULL)
  599.         {
  600.             m_pTool->Release ();
  601.             m_pTool = NULL;
  602.         }
  603.  
  604.         if(m_pRegMets != NULL)
  605.         {
  606.             m_pRegMets->Release();
  607.             m_pRegMets = NULL;
  608.         }
  609.         
  610.         CViewWnd *pView = (CViewWnd *)GetDlgItem(IDC_PREVIEW);
  611.         pView->ClearAll();
  612.     }
  613.     CDialog::OnSysCommand(nID, lParam );
  614.  
  615. }
  616.  
  617. ///------------------------------------------
  618. // DragOutline functions
  619. BOOL CSdiDialog::BeginDrag(const CPoint& point)
  620. {
  621.     VariantClear(&m_dragOutline);
  622.     SAFEARRAYBOUND bound;
  623.     bound.cElements = 0;
  624.     bound.lLbound = 0;
  625.     m_dragOutline.vt = VT_ARRAY|VT_R8;
  626.     m_dragOutline.parray = ::SafeArrayCreate(VT_R8, 1, &bound);
  627.  
  628.     BoundingBox* Box = NULL;
  629.     IVertex* vMax = NULL;
  630.     IVertex* vMin = NULL;
  631.     double x, y, x1, y1;
  632.     x = x1 = y = y1 = 0;
  633.     try
  634.     {
  635.         HRESULT hRes = m_pDragGraphic->CreateDragOutline(&m_dragOutline);
  636.         CHECK_HRESULT(hRes)
  637.  
  638.         m_bDragging = TRUE;
  639.         m_pDragGraphic->put_Visible(FALSE);
  640.         VARIANT varState;
  641.         varState.vt = VT_I4;
  642.         varState.lVal = imsiDragBegin;
  643.  
  644.         hRes = m_pDragGraphic->CalcBoundingBox(NULL, &Box);
  645.         CHECK_HRESULT(hRes)
  646.  
  647.         hRes = Box->get_Max(&vMax);
  648.         CHECK_HRESULT(hRes)
  649.  
  650.         hRes = Box->get_Min(&vMin);
  651.         CHECK_HRESULT(hRes)
  652.  
  653.         hRes = vMax->get_X(&x);
  654.         CHECK_HRESULT(hRes)
  655.  
  656.         hRes = vMax->get_Y(&y);
  657.         CHECK_HRESULT(hRes)
  658.  
  659.         hRes = vMin->get_X(&x1);
  660.         CHECK_HRESULT(hRes)
  661.  
  662.         hRes = vMin->get_Y(&y1);
  663.         CHECK_HRESULT(hRes)
  664.  
  665.         x = x1 + (x - x1)/2;
  666.         y = y1 + (y - y1)/2;
  667.         hRes = m_pView->ViewToScreen(x, y, &x1, &y1, NULL);
  668.         CHECK_HRESULT(hRes)
  669.  
  670.         hRes = m_pView->DragOutline(&m_dragOutline, x1, y1, &varState);
  671.         CHECK_HRESULT(hRes)
  672.         
  673.  
  674.         varState.lVal = imsiDragContinue;
  675.         hRes = m_pView->DragOutline(&m_dragOutline, point.x, point.y, &varState);
  676.         CHECK_HRESULT(hRes)
  677.     }
  678.     catch (...)
  679.     {
  680.         TRACE_EXCEPTION("CSdiDialog::BeginDrag")
  681.     }
  682.     if (vMin != NULL)
  683.     {
  684.         vMin->Release();
  685.         vMin = NULL;
  686.     }
  687.     if (vMax != NULL)
  688.     {
  689.         vMax->Release();
  690.         vMax = NULL;
  691.     }
  692.     if (Box != NULL)
  693.     {
  694.         Box->Release();
  695.         Box = NULL;
  696.     }
  697.  
  698.     return TRUE;
  699. }
  700.  
  701. void CSdiDialog::Drag(const CPoint& point)
  702. {
  703.     if (m_pView == NULL)
  704.         return;
  705.  
  706.     VARIANT varState;
  707.     varState.vt = VT_I4;
  708.     varState.lVal = imsiDragContinue;
  709.  
  710.     try
  711.     {
  712.         HRESULT hRes = m_pView->DragOutline(&m_dragOutline, point.x, point.y, &varState);
  713.         CHECK_HRESULT(hRes)
  714.     }
  715.     catch (...)
  716.     {
  717.         TRACE_EXCEPTION("CSdiDialog::Drag")
  718.     }
  719.     
  720. }
  721.  
  722. void CSdiDialog::EndDrag(const CPoint& point)
  723. {
  724.     m_bDragging = FALSE;
  725.     if (m_pView == NULL || m_pDragGraphic == NULL)
  726.         return;
  727.  
  728.     VARIANT varState;
  729.     varState.vt = VT_I4;
  730.     varState.lVal = imsiDragEnd;
  731.     IMatrix *pMat = NULL;
  732.  
  733.     try 
  734.     {
  735.         HRESULT hRes = m_pView->DragOutline(&m_dragOutline, point.x, point.y, &varState);
  736.         CHECK_HRESULT(hRes)
  737.  
  738.         VariantClear(&m_dragOutline);
  739.  
  740.         double dx, dy, dz;
  741.         dx = dy = dz = 0;
  742.  
  743.         double dxWorld, dyWorld, dzWorld;
  744.         dxWorld = dyWorld = dzWorld = 0;
  745.         hRes = m_pView->ScreenToView(point.x, point.y, &dx, &dy);
  746.         CHECK_HRESULT(hRes)
  747.  
  748.         VARIANT var;
  749.         var.vt = VT_ERROR;
  750.         var.scode = DISP_E_PARAMNOTFOUND;
  751.  
  752.         ImsiSpaceModeType curMode;    
  753.  
  754.         hRes = m_pView->get_SpaceMode(&curMode);
  755.         CHECK_HRESULT(hRes)
  756.         dxWorld = dx;
  757.         dyWorld = dy; 
  758.  
  759.         if(curMode == imsiModelSpace)
  760.         {
  761.             hRes = m_pView->ViewToWorld(dx, dy, dz, &dxWorld, &dyWorld, &dzWorld);
  762.             CHECK_HRESULT(hRes)
  763.         }
  764.         hRes = m_pDragGraphic->MoveAbsolute(dxWorld, dyWorld, 0, &var, &var, &var, &pMat);
  765.         CHECK_HRESULT(hRes)
  766.         m_pDragGraphic->put_Visible(TRUE);
  767.     }
  768.     catch (...)
  769.     {
  770.         TRACE_EXCEPTION("CSdiDialog::EndDrag")
  771.     }
  772.  
  773.  
  774.     if(pMat != NULL)
  775.     {
  776.         pMat->Release();
  777.         pMat = NULL;
  778.     }
  779.     if (m_pDragGraphic != NULL)
  780.     {
  781.         m_pDragGraphic->Release();
  782.         m_pDragGraphic = NULL;
  783.     }
  784.     
  785. }
  786.  
  787. void CSdiDialog::RestartDrag(const CPoint& point)
  788. {
  789.     try
  790.     {
  791.         m_bDragging = FALSE;
  792.         VARIANT varState;
  793.         varState.vt = VT_I4;
  794.         varState.lVal = imsiDragEnd;
  795.         HRESULT hRes = m_pView->DragOutline(&m_dragOutline, point.x, point.y, &varState);
  796.         CHECK_HRESULT(hRes)
  797.  
  798.         VariantClear(&m_dragOutline);
  799.         BeginDrag(point);
  800.     }
  801.     catch (...)
  802.     {
  803.         TRACE_EXCEPTION("CSdiDialog::RestartDrag")
  804.     }
  805.  
  806. }
  807.  
  808. void CSdiDialog::DrawingBeforeClose(LPDISPATCH WhichDrawing, BOOL FAR* Cancel)
  809. {
  810.     *Cancel = FALSE;
  811.     Drawings *pDrs = NULL;
  812.     long n = 0;
  813.  
  814.     try
  815.     {
  816.         HRESULT hRes = m_pIApplication->get_Drawings(&pDrs);
  817.         CHECK_HRESULT(hRes)
  818.         hRes = pDrs->get_Count(&n);
  819.         CHECK_HRESULT(hRes)
  820.  
  821.         if (n == 2)
  822.         {
  823.  
  824.         if(m_dwEventConnection != 0)
  825.             DisconnectEvents();
  826.             m_pTool->m_bRunned = FALSE;
  827.  
  828.             CViewWnd *pView = (CViewWnd *)GetDlgItem(IDC_PREVIEW);
  829.             pView->ClearAll();
  830.             CSdiDialog::OnClose();
  831.         }
  832.  
  833.     }
  834.  
  835.     catch (...)
  836.     {
  837.  
  838.         TRACE_EXCEPTION("CSdiDialog::DrawingBeforeClose")
  839.     }
  840.     if (pDrs != NULL)
  841.     {
  842.         pDrs->Release();
  843.         pDrs = NULL;
  844.     }
  845.  
  846.  
  847. }
  848.